package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.entity.SteelMattertype;
import com.sky.mapper.SteelMattertypeMapper;
import com.sky.service.SteelMattertypeService;
import com.sky.vo.SteelMattertypeVo;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author 23714
* @description 针对表【steel_mattertype】的数据库操作Service实现
* @createDate 2024-09-29 10:45:16
*/
@Service
public class SteelMattertypeServiceImpl extends ServiceImpl<SteelMattertypeMapper, SteelMattertype>
    implements SteelMattertypeService{

    @Resource
    private SteelMattertypeMapper steelMattertypeMapper;

    /**
     * 获取所有类型 一级包含二级
     * @return
     */
    @Override
    public List<SteelMattertypeVo> getAll() {
        // 先获取所有一级类型
        QueryWrapper<SteelMattertype> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SteelMattertype::getMattertypeParentid,null);
        final val list = steelMattertypeMapper.selectList(queryWrapper);

        // Vo 集合
        List<SteelMattertypeVo> list1=new ArrayList<>();


        // 循环一级获取所对象的二级
        for (SteelMattertype steelMattertype : list) {
            QueryWrapper<SteelMattertype> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.lambda().eq(SteelMattertype::getMattertypeParentid,steelMattertype.getId());
            final val list2 = steelMattertypeMapper.selectList(queryWrapper2);


            SteelMattertypeVo sVo=new SteelMattertypeVo();
            // 拷贝对象 steelMattertype -> sVo
            BeanUtils.copyProperties(steelMattertype,sVo);
            sVo.setChildren(list2);
            list1.add(sVo);
        }
        return list1;
    }

}




